package com.payment.common.base;


import com.payment.common.utils.PropertiesUtil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisPool {
	private static JedisPool pool;
	private static Integer maxTotal=Integer.parseInt(PropertiesUtil.getProperty("redis.max.total"));
	private static Integer maxIdle=Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle"));
	private static Integer minIdle=Integer.parseInt(PropertiesUtil.getProperty("redis.min.idle"));
	private static boolean testOnBorrow=Boolean.parseBoolean(PropertiesUtil.getProperty("redis.max.total"));
    
	private static String redisIp=PropertiesUtil.getProperty("redis1.ip");
	private static int redisPort=Integer.parseInt(PropertiesUtil.getProperty("redis1.port"));

	
	private static void initPool() {
		//设置连接池配置参数
		JedisPoolConfig config=new JedisPoolConfig();
		config.setMaxTotal(maxTotal);
		config.setMaxIdle(maxIdle);
		config.setMinIdle(minIdle);
		config.setMinEvictableIdleTimeMillis(3000);
		config.setTestOnBorrow(testOnBorrow);
		//初始化连接池
		pool=new JedisPool(config,redisIp,redisPort,2000,PropertiesUtil.getProperty("redis1.pwd"),15);
		//pool=new JedisPool(config, redisIp, redisPort, 1000*2, PropertiesUtil.getProperty("redis.pwd"), 10);
	}
	
	
	static {
		//静态初始化 redis
		initPool();
	}
	
	public static Jedis getJedis() {
		//从连接池获取实例
		return pool.getResource();
	}
	
	public static JedisPool getJedisPool() {
		//从连接池获取实例
		return pool;
	}
	
	public static void main(String[] args) {
		Jedis jedis=pool.getResource();
		jedis.set("fuzhili","fuzhili");
//		returnResource(jedis);
//		pool.destroy();
//		System.out.println("end");
//		System.out.println(jedis.get("fuzhili"));
		//RedisPoolUtil.del("17778255670Time");
	}
	
}
